/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonInfrastructure.accountManagement.session;

import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.accountManagement.entity.RoleEntity;
import commonInfrastructure.system.session.AuditTrailSessionBean;
import commonInfrastructure.system.session.SystemSessionBean;
import java.text.ParseException;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class RoleSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    private EntityManager em;
    
    @EJB
    SystemSessionBean systemSession = new SystemSessionBean();
    @EJB
    AuditTrailSessionBean auditSession = new AuditTrailSessionBean();
    
    public RoleEntity getRole(Long RoleId)
    {
        Query query = em.createQuery("SELECT u FROM RoleEntity u WHERE u.id = :inRoleId");
        query.setParameter("inRoleId", RoleId);
        
        RoleEntity role = null;
        try{
            role = (RoleEntity)query.getSingleResult();
        }
        catch(NoResultException ex){
            ex.printStackTrace();
        }
        return role;
    }
    
    public RoleEntity getRoleByName(String name){
        Query query = em.createQuery("SELECT u FROM RoleEntity u WHERE u.roleName = :inRoleName");
        query.setParameter("inRoleName",name);
        
        RoleEntity role = null;
        try{
            role = (RoleEntity)query.getSingleResult();
        }
        catch(NoResultException ex){
            ex.printStackTrace();
        }
        
        return role;
    }
    
    public void addRole(RoleEntity role) throws ParseException
    {
        em.persist(role);
        em.flush();
        //Audit Trail
//        EmployeeEntity user = systemSession.getCurrentUser();
//        auditSession.addAuditTrail(systemSession.getToday(), role.getId(), user.getUserName(), "commonInfrastructure.accountManagement", "Role", "Add", "");
    }
    public void editRole(RoleEntity role) throws ParseException
    {
        em.merge(role);
        //Audit Trail
        EmployeeEntity user = systemSession.getCurrentUser();
        auditSession.addAuditTrail(systemSession.getToday(), role.getId(), user.getUserName(), "commonInfrastructure.accountManagement", "Role", "Edit", "");
    }
    
    public void removeRole(Long RoleId) throws ParseException
    {
        RoleEntity role = em.find(RoleEntity.class, RoleId);
        em.remove(role);
        //Audit Trail
        EmployeeEntity user = systemSession.getCurrentUser();
        auditSession.addAuditTrail(systemSession.getToday(), RoleId, user.getUserName(), "commonInfrastructure.accountManagement", "Role", "Delete", "");
    }
    
    public List<RoleEntity> getAllRoles(){
        Query query = em.createQuery("SELECT s1 FROM RoleEntity s1");
        return query.getResultList();
    }
    
    
}
